package com.example.download.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.download.bean.downloading_item
import com.example.download.R

class downloading_itemAdapter (private val downloadingitemList: MutableList<downloading_item>,
): RecyclerView.Adapter<downloading_itemAdapter.ViewHolder>(){
    class ViewHolder(view: android.view.View): RecyclerView.ViewHolder(view) {
        val iv_dli_item_imageView: android.widget.ImageView = view.findViewById(R.id.iv_dli_item_imageView)
        val iv_dli_item_title: android.widget.TextView = view.findViewById(R.id.iv_dli_item_title)
        val iv_dli_item_date: android.widget.TextView = view.findViewById(R.id.iv_dli_item_date)
        val iv_dli_item_speed: android.widget.TextView = view.findViewById(R.id.iv_dli_item_speed)
        val iv_dli_item_close: android.widget.ImageView = view.findViewById(R.id.iv_dli_item_close)
        val iv_dli_item_retry: android.widget.ImageView = view.findViewById(R.id.iv_dli_item_retry)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.downloading_item, parent, false)
        return ViewHolder(view)
    }
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val downloadingitem = downloadingitemList[position]
        holder.iv_dli_item_imageView.setImageResource(downloadingitem.iv_dli_item_imageView?.toInt()?:0)
        holder.iv_dli_item_title.text = downloadingitem.iv_dli_item_title
        holder.iv_dli_item_date.text = downloadingitem.iv_dli_item_date
        holder.iv_dli_item_speed.text = downloadingitem.iv_dli_item_speed
        holder.iv_dli_item_close.setImageResource(downloadingitem.iv_dli_item_close?.toInt()?:0)
        holder.iv_dli_item_retry.setImageResource(downloadingitem.iv_dli_item_retry?.toInt()?:0)


        if (position == 0) {
            holder.iv_dli_item_speed.setTextColor(Color.RED)
        }
    }
    override fun getItemCount(): Int {
        return downloadingitemList.size
    }
    fun updateData(newList: List<downloading_item>) {
        downloadingitemList.clear()
        downloadingitemList.addAll(newList)
        notifyDataSetChanged()
    }
}